package com.terren.statistic.common.entity;

import java.io.File;
import java.io.InputStream;
import java.util.Properties;
import org.apache.log4j.Logger;

import com.terren.statistic.common.util.CommonUtil;
import com.terren.statistic.common.util.LogUtil;

public class SysConfig {
	static Logger log = LogUtil.getLogger(SysConfig.class);
	
	public static String PROJECT_PATH;
	public static String LOG4J_PATH;
	public static Integer LIMIT;//接口每次调取数量
	public static String OUTSIDE_PATH;   // 文件输出路径
	// RIDX Server
	public static String RIDX_HOST;
	public static Integer RIDX_PORT;
	
	public static String FILE_FORMAT;//导出文件格式
	public static Boolean FILE_CREATE;//调用接口后是否生成落地文件
	
	static {
		
		PROJECT_PATH = CommonUtil.getProjectPath(SysConfig.class)+File.separator;
		LOG4J_PATH =  PROJECT_PATH+"config"+File.separator+"log4j"+File.separator+"log4j.xml";
		
		/**
		 * 初始化配置文件内容
		 */
		Properties properties = getSysConfigProperties();
		/***** 读取配置文件 *****/
		
		String limit = properties.getProperty("limit");
		if (null != limit) {
			LIMIT = Integer.parseInt(limit);
		}
		
		String outsidePath = properties.getProperty("outside.path");
		if (null != outsidePath) {
			OUTSIDE_PATH = outsidePath;
		}
		
		String ridxHost = properties.getProperty("ridx.host");
		if (null != ridxHost) {
			RIDX_HOST = ridxHost;
		}
		
		String ridxPort = properties.getProperty("ridx.port");
		if (null != ridxPort) {
			RIDX_PORT = Integer.parseInt(ridxPort);
		}
		
		String fileFormat  = properties.getProperty("file.format");
		if (null != fileFormat) {
			FILE_FORMAT = fileFormat;
		}
		
		String fileCreate = properties.getProperty("file.create");
		if (null != fileCreate) {
			SysConfig.FILE_CREATE = Boolean.valueOf(fileCreate);
		}
	}
	
	/**
	 * 读取配置文件内容
	 */
	private static Properties getSysConfigProperties() {
		InputStream resourceAsStream = null;
		String configPath = "config" + File.separator + "sysConfig.properties";
		resourceAsStream = CommonUtil.class.getClassLoader().getResourceAsStream(configPath);
		if (resourceAsStream == null) {
			log.error("没有找到配置文件:"+configPath);
            return null;
        }
        Properties properties = new Properties();
        try {
            properties.load(resourceAsStream);
        }catch (Exception e) {
        	log.error("读取配置文件错误:"+"config" + File.separator + "sysConfig.properties",e);
		}
        return properties;
	}
}
